package com.mercury.ec.framework.air.core.client.impl.sunstn.jaxb.adapter;

import cn.hutool.core.util.StrUtil;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * jaxb LocalDate适配器
 *
 * @author hesx
 */
public class LocalDate2XmlAdapter extends XmlAdapter<String, LocalDate> {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @Override
    public LocalDate unmarshal(String xml) throws Exception {
        if (StrUtil.isBlank(xml)) {
            return null;
        }
        return LocalDate.parse(xml, formatter);
    }

    @Override
    public String marshal(LocalDate localDate) throws Exception {
        if (localDate == null) {
            return "";
        }
        return localDate.format(formatter);
    }
}
